home *** CD-ROM | disk | FTP | other *** search
- /* AuditFileDialog.c */
- /*
- * AuditFileDialog.c
- * Copyright © 1995, Apple Computer Inc. All Rights Reserved.
- */
- #include "DisplayNameRegistry.h"
-
- #define kEndOfLine 0x0D /* Return */
- #define TestTDFlag(elementHdl, mask) (((**elementHdl).flag & (mask)) != 0)
-
- /*
- * Standard File dialog items. This follows the layout of the old Standard File
- * (-3999) DITL. Our private items are at the end.
- */
- enum { /* Save File Dialog */
- kSaveButton = 1,
- kDontSaveButton,
- kSaveAsButton,
- kFolderHierarchyPopup,
- kEjectButton,
- kDriveButton,
- kFileNameText,
- kCatalogList,
- kSaveAllRadio,
- kSaveVisibleRadio
- };
-
- OSErr WriteVisibleNameRegistry(
- TwistDownHdl twistDownHandle
- );
- OSErr WriteEntireNameRegistry(
- TwistDownHdl twistDownHandle
- );
- OSErr WriteThisElement(
- TwistDownHdl twistDownHandle
- );
- void SaveFileDialog(
- short dialogID,
- ConstStr255Param promptString,
- ConstStr255Param originalName,
- SFReply *reply
- );
- static pascal short PutDialogFilter(
- short itemHit,
- DialogPtr theDialog
- );
- static void SelectDialogRadio(
- DialogPtr theDialog,
- short selectedButton
- );
- void CheckForNullByte( /* Debug only */
- Ptr dataPtr,
- long dataLength
- );
-
- /*
- * Create an output file. .
- */
- void
- CreateOutputFile(void)
- {
- OSErr status;
- OSErr closeStatus;
- TwistDownHdl listHead;
- Cell theCell;
-
- SaveFileDialog(
- DLOG_SFPutFile,
- "\pOutput File",
- "\pName Registry Output",
- &gSFReply
- );
- if (gSFReply.good == FALSE)
- status = userCanceledErr;
- else {
- /*
- * Create the file, elmininating any duplicate.
- */
- SetCursor(*GetCursor(watchCursor));
- status = Create(gSFReply.fName, gSFReply.vRefNum, 'TTXT', 'TEXT');
- if (status == dupFNErr) { /* Exists already? */
- status = FSDelete(gSFReply.fName, gSFReply.vRefNum);
- if (status == noErr) {
- status = Create(
- gSFReply.fName, gSFReply.vRefNum, 'TTXT', 'TEXT');
- }
- }
- if (status == noErr) {
- status = FSOpen(
- gSFReply.fName, gSFReply.vRefNum, &gSaveFileRefNum);
- }
- if (status != noErr) {
- if (status != userCanceledErr)
- NonFatalError(status, "\pCan't create file");
- }
- else {
- SetPt(&theCell, 0, 0);
- listHead = GetTwistDownElementHandle(
- gCurrentBrowserPtr->theList, theCell);
- if (gSaveAllElements)
- status = WriteEntireNameRegistry(listHead);
- else {
- status = WriteVisibleNameRegistry(listHead);
- }
- if (status != noErr)
- NonFatalError(status, "\pCan't write file");
- closeStatus = FSClose(gSaveFileRefNum);
- if (status == noErr && closeStatus != noErr) {
- NonFatalError(closeStatus, "\pCan't close file");
- status = closeStatus;
- }
- closeStatus = FlushVol(NULL, gSFReply.vRefNum);
- if (status == noErr && closeStatus != noErr)
- NonFatalError(closeStatus, "\pCan't flush volume");
- }
- InitCursor();
- }
- }
-
- OSErr
- WriteVisibleNameRegistry(
- TwistDownHdl twistDownHandle
- )
- {
- OSErr status;
-
- status = noErr;
- while (status == noErr && twistDownHandle != NULL) {
- status = WriteThisElement(twistDownHandle);
- if (status == noErr
- && (**twistDownHandle).subElement != NULL
- && TestTDFlag(twistDownHandle, kShowSublist))
- status = WriteVisibleNameRegistry((**twistDownHandle).subElement);
- twistDownHandle = (**twistDownHandle).nextElement;
- }
- return (status);
- }
-
- OSErr
- WriteEntireNameRegistry(
- TwistDownHdl twistDownHandle
- )
- {
- OSErr status;
-
- status = noErr;
- while (status == noErr && twistDownHandle != NULL) {
- status = WriteThisElement(twistDownHandle);
- if (status == noErr && (**twistDownHandle).subElement != NULL)
- status = WriteEntireNameRegistry((**twistDownHandle).subElement);
- twistDownHandle = (**twistDownHandle).nextElement;
- }
- return (status);
- }
-
- /*
- * Write one line of text to the output file.
- */
- OSErr
- WriteThisElement(
- TwistDownHdl twistDownHdl
- )
- {
- OSErr status;
- long textLength;
- short handleState;
- static char gEndOfLine[1] = { kEndOfLine };
- #define ELEM (**twistDownHdl)
-
- handleState = HGetState((Handle) twistDownHdl);
- HLock((Handle) twistDownHdl);
- textLength = ELEM.dataLength;
- if (textLength == 0)
- status = noErr;
- else {
- CheckForNullByte((Ptr) ELEM.data, ELEM.dataLength);
- status = FSWrite(gSaveFileRefNum, &textLength, ELEM.data);
- }
- HSetState((Handle) twistDownHdl, handleState);
- if (status == noErr) {
- textLength = 1;
- status = FSWrite(gSaveFileRefNum, &textLength, gEndOfLine);
- }
- if (status != noErr)
- NonFatalError(status, "\pWriteThisElement");
- return (status);
- }
-
- void
- SaveFileDialog(
- short dialogID,
- ConstStr255Param promptString,
- ConstStr255Param originalName,
- SFReply *reply
- )
- {
- static DlgHookUPP dlgHookUPP = NULL;
- Point where;
-
- SetPt(&where, 80, 80);
- if (dlgHookUPP == NULL)
- dlgHookUPP = NewDlgHookProc(PutDialogFilter);
- SFPPutFile(
- where,
- promptString,
- originalName,
- dlgHookUPP,
- reply,
- dialogID,
- NULL /* No Modal Dialog Filter */
- );
- }
-
- static pascal short
- PutDialogFilter(
- short itemHit,
- DialogPtr theDialog
- )
- {
- short itemType;
- Handle itemHandle;
- Rect itemRect;
-
- switch (itemHit) {
- case sfHookFirstCall:
- SelectDialogRadio(
- theDialog,
- (gSaveAllElements) ? kSaveAllRadio : kSaveVisibleRadio
- );
- break;
- case kSaveAllRadio:
- case kSaveVisibleRadio:
- SelectDialogRadio(theDialog, itemHit);
- break;
- case kSaveButton:
- GetDialogItem(
- theDialog,
- kSaveAllRadio,
- &itemType,
- &itemHandle,
- &itemRect
- );
- gSaveAllElements = GetControlValue((ControlHandle) itemHandle);
- }
- return (itemHit);
- }
-
-
- static void
- SelectDialogRadio(
- DialogPtr theDialog,
- short selectedButton
- )
- {
- short itemType;
- Handle itemHandle;
- Rect itemRect;
- short i;
-
- for (i = kSaveAllRadio; i <= kSaveVisibleRadio; i++) {
- GetDialogItem(
- theDialog,
- i,
- &itemType,
- &itemHandle,
- &itemRect
- );
- SetControlValue(
- (ControlHandle) itemHandle,
- (i == selectedButton) ? 1 : 0
- );
- }
- }
-
- void
- CheckForNullByte(
- Ptr dataPtr,
- long dataLength
- )
- {
- long i;
-
- for (i = 0; i < dataLength; i++) {
- if (dataPtr[i] == 0) {
- printf("%ld in %ld \"%.*s\"\n", i, dataLength, dataLength, dataPtr);
- break;
- }
- }
- }
-
-